package com.pagepub.expo.manager;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.pagepub.comm.util.FormatDateTime;
import com.pagepub.expo.dao.CorpPublicationDao;
import com.pagepub.expo.entity.AppUser;
import com.pagepub.expo.entity.CorpCatalog;
import com.pagepub.expo.entity.CorpPublication;
import com.pagepub.expo.entity.IdName;

@Service
@Transactional
@RemotingDestination(channels = { "my-amf", "my-secure-amf" })
public class CorpPublicationMng {
	@Autowired
	private CorpPublicationDao corpPublicationDao;


	@RemotingInclude
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<CorpPublication> findAll() {
		return corpPublicationDao.findAll();
	}
	
	@RemotingInclude
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public CorpPublication findById(int id){
		return corpPublicationDao.findById(id);
	}

	
	@RemotingInclude
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public int getCount(Map param){
		return corpPublicationDao.getCount(param);
	}
	
	
	
	
	@RemotingInclude
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public boolean pubSizeAndIsauditingUpdate(int pubfilesize,int pubpagesize,int publicationid,boolean sign,int isauditing){
		return corpPublicationDao.pubSizeAndIsauditingUpdate(pubfilesize, pubpagesize, publicationid, sign, isauditing);
	}
	
	
	
	
	@RemotingInclude
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<CorpPublication> findByPage(int currentPage,int pageSize,Map param){
		return corpPublicationDao.findByPage(currentPage, pageSize, param);
	}

	

	@RemotingInclude
	public CorpPublication add(CorpPublication corpPublication) {
		corpPublicationDao.insert(corpPublication);
		return corpPublicationDao.findById(corpPublication.getId());
	}

	@RemotingInclude
	public Long remove(Long id) {
		corpPublicationDao.delete(id);
		return id;
	}

	@RemotingInclude
	public CorpPublication update(CorpPublication corpPublication) {
		corpPublicationDao.update(corpPublication);
		return corpPublicationDao.findById(corpPublication.getId());
	}
	
	@RemotingInclude
	public boolean updatePublicationCount(CorpPublication corpPublication) {
		boolean flag=false;
			flag=corpPublicationDao.updatePublicationCount(corpPublication);
		return flag;
	}
	
	@RemotingInclude
	public boolean isDeletePublication(String corpId, int publicationid) {
		boolean flag=false;
		flag=corpPublicationDao.isDeletePublication(corpId, publicationid);
	return flag;
	}
	
	public String getPulicationName(String corpId){		
		   String  filename=FormatDateTime.getToday("yyyyMM");
		   CorpPublication publication=corpPublicationDao.getPubMagazineCount(corpId);
		   if(publication!=null){
			   filename=Long.toString(publication.getPubMagazineCount()+1);
		   }
		   else
			   filename=filename+"01";
			return filename;
		}
	
	@RemotingInclude
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<IdName> findIdNames(){
		return corpPublicationDao.findIdNames();
	}
	
	@RemotingInclude
	public void audit(Long id, boolean pass) {
		corpPublicationDao.audit(id, pass);
	}
}
